package com.hspedu.newData.dmsxl.滑动窗口;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-29 
 * @Version: 1.0
 */

public class LC2302统计得分小于k的子数组数目 {

    // todo 有个方法可以使用乘积的方式
    public long countSubarrays(int[] nums, long k) {
        long res = 0;

        if (nums[0] < k){
            res += 1;
        }

        int slow = 0;
        long sum = 0;
        long count = nums[0];

        for (int i = 1; i < nums.length; i++) {
            if (nums[i] < k){
                res += 1;
            }

            count += nums[i];
            sum = count * (i - slow + 1);

            if (sum < k && slow < i){
                res += i - slow;
                continue;
            }

            while (sum >= k && slow < i){
                count -= nums[slow++];
                sum = count*(i-slow+1);
                if (sum < k && slow != i){
                    res += i - slow;
                }
            }

        }
        return res;
    }

    @Test
    public void test() {
        // 测试代码

        long l = countSubarrays(new int[]{2,1,4,3,5}, 10);
        System.out.println(l);

    }
}
